<?php
namespace app\index\controller;

use app\BaseController;
use think\facade\Db;

class User extends BaseController
{
  public function addr_list() {
    $user = json_decode(session('user'), true);
    if (!$user) {
      returnJson(-2, '请先登录！');
    }

    $list = Db::table('user_addr')->order('id', 'desc')->where('user_id', $user['id'])->select();
    if (!$list) {
      returnJson(-2, '查询失败！');
    }
    
    returnJson(0, '成功！', $list);
  }

  public function add_addr() {
    $request = request()->param();

    $user = json_decode(session('user'), true);
    if (!$user) {
      returnJson(-2, '请先登录！');
    }

    $data = [
      'name' => request()->param('name'),
      'mobile' => request()->param('mobile'),
      'address' => request()->param('address'),
      'user_id' => $user['id']
    ];
    
    $update = Db::table('user_addr')->save($data);
    if ($update >= 0) {
      returnJson(0, '添加成功！');
    }
    returnJson(-2, '添加失败！');
  }

  
  public function del_addr () {
    $ids = request()->param('ids');
    $idsArr = explode(',', $ids);
    
    if (is_array($idsArr)){
      $del = Db::table('user_addr')->delete($idsArr);
      returnJson(0, '删除成功！');
    }
    returnJson(-2, '删除失败！');
  }



  public function order_list() {

    $user = json_decode(session('user'), true);
    if (!$user) {
      returnJson(-2, '请先登录！');
    }

    $list = Db::table('shop_order')->order('id', 'desc')->where('user_id', $user['id'])->select();
    if (!$list) {
      returnJson(-2, '查询失败！');
    }
    
    returnJson(0, '成功！', $list);
  }

  public function order_goods_list() {
    $order_id = request()->param('order_id');

    $user = json_decode(session('user'), true);
    if (!$user) {
      returnJson(-2, '请先登录！');
    }

    $list = Db::table('shop_order_goods')->where('order_id', $order_id)->select();
    if (!$list) {
      returnJson(-2, '查询失败！');
    }
    
    returnJson(0, '成功！', $list);
  }


  public function order_status() {
    $id = request()->param('id');
    $status = request()->param('status');
    if (!$id || !$status ) {
      returnJson(-2, '参数错误！');
    }

    $user = json_decode(session('user'), true);
    if (!$user) {
      returnJson(-2, '请先登录！');
    }

    // $order = Db::table('shop_order')->where('id', $id)->find();

    $res = Db::table('shop_order')->where('id', $id)->where('user_id', $user['id'])->update(['status' => $status]);
    if (!$res) {
      returnJson(-2, '订单操作失败！');
    }
    
    if ($status == 2) {
      $goods = Db::table('shop_order_goods')->where('order_id', $id)->select();
      if ($goods) {
        foreach ($goods as $key => $value) {
          Db::table('shop_goods')->where('id', $value['goods_id'])->dec('number', $value['goods_num'])->update();
        }
      }
    }
    returnJson(0, '成功！');
  }

  // 收藏
  public function collect() {
    $user = json_decode(session('user'), true);
    if (!$user) {
      returnJson(-2, '请先登录！');
    }
    $list = Db::table('shop_collect')
    ->alias('c')
    ->join('shop_goods g','c.goods_id = g.id')
    ->where('c.uid', $user['id'])
    ->where('c.status', 1)
    ->select();
    
    if (!$list) {
      returnJson(-2, '查询失败！');
    }

    returnJson(0, '成功！', $list);

  }



}